﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace elr_tracking_bal.CAT
{
    public class Empresa : INotifyPropertyChanged
    {
        private int _ID_EMPRESA;
        private string _EMPRESA_NAME;
        private string _RAZON_SOCIAL;
        private string _RFC;
        private string _DIRECCION;

        public int ID_EMPRESA
        {
            get
            {
                return _ID_EMPRESA;
            }
            set
            {
                if (_ID_EMPRESA != value)
                {
                    _ID_EMPRESA = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_EMPRESA"));
                    }
                }
            }
        }

        public string EMPRESA_NAME
        {
            get
            {
                return _EMPRESA_NAME;
            }
            set
            {
                if (_EMPRESA_NAME != value)
                {
                    _EMPRESA_NAME = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("EMPRESA_NAME"));
                    }
                }
            }
        }

        public string RAZON_SOCIAL
        {
            get
            {
                return _RAZON_SOCIAL;
            }
            set
            {
                if (_RAZON_SOCIAL != value)
                {
                    _RAZON_SOCIAL = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("RAZON_SOCIAL"));
                    }
                }
            }
        }

        public string RFC
        {
            get
            {
                return _RFC;
            }
            set
            {
                if (_RFC != value)
                {
                    _RFC = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("RFC"));
                    }
                }
            }
        }

        public string DIRECCION
        {
            get
            {
                return _DIRECCION;
            }
            set
            {
                if (_DIRECCION != value)
                {
                    _DIRECCION = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("DIRECCION"));
                    }
                }
            }
        }

        /// <summary>
        /// Método que ejecuta el store para ontener las Empresas
        /// </summary>
        /// <returns></returns>
        public static DataTable GetData()
        {
            DataTable dt;
            dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETLIST_CAT_EMPRESA");
            return dt;
        }
        /// <summary>
        /// Método que obtiene la lista de Empresas
        /// </summary>
        /// <returns></returns>
        public static List<Empresa> GetData_Empresa()
        {
            try
            {
                DataTable dt;
                dt = Empresa.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Empresa> list = dt.AsEnumerable().Select(
                        row => new Empresa
                        {
                            ID_EMPRESA = row.Field<int?>("ID_EMPRESA").GetValueOrDefault(),
                            EMPRESA_NAME = String.IsNullOrEmpty(row.Field<string>("EMPRESA_NAME")) ? "" : row.Field<string>("EMPRESA_NAME"),
                            RAZON_SOCIAL = String.IsNullOrEmpty(row.Field<string>("RAZON_SOCIAL")) ? "" : row.Field<string>("RAZON_SOCIAL"),
                            RFC = String.IsNullOrEmpty(row.Field<string>("RFC")) ? "" : row.Field<string>("RFC"),
                            DIRECCION = String.IsNullOrEmpty(row.Field<string>("DIRECCION")) ? "" : row.Field<string>("DIRECCION"),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static ObservableCollection<Empresa> GetDataEmpresa()
        {
            try
            {
                DataTable dt;
                dt = Empresa.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    ObservableCollection<Empresa> list = new ObservableCollection<Empresa>();
                    for (int x = 0; x < dt.Rows.Count; x++)
                    {
                        int id_Empresa = Convert.ToInt32(dt.Rows[x][0]);
                        string empresa = dt.Rows[x][1].ToString();
                        string razonSocial = dt.Rows[x][1].ToString();
                        string rfc = dt.Rows[x][1].ToString();
                        string direccion = dt.Rows[x][1].ToString();

                        Empresa emp = new Empresa(id_Empresa,empresa, razonSocial, rfc, direccion);
                        list.Add(emp);

                    }

                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Método para insertar una Empresa
        /// </summary>
        /// <param name="em"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg InsertEmpresa(Empresa cat)
        {
            Empresa inser;
            elr_tracking_bal.UpsertStateMsg inserCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_INSERT_CAT_EMPRESA '" + cat.EMPRESA_NAME + "','" + cat.RAZON_SOCIAL + "','"
                                                                + cat.RFC + "','" + cat.DIRECCION + "'");

                inser = new Empresa(cat.EMPRESA_NAME, cat.RAZON_SOCIAL, cat.RFC, cat.DIRECCION);
                inserCat = new elr_tracking_bal.UpsertStateMsg("Registros guardados " + dt.ToString(), true);
                return inserCat;
            }
            catch (Exception ex)
            {
                inserCat = new elr_tracking_bal.UpsertStateMsg("Error al guardar " + ex, false);
                return inserCat;
            }
        }
        /// <summary>
        /// Método para eliminar Empresas
        /// </summary>
        /// <param name="ID_EMPRESA"></param>
        /// <returns></returns>
        public static elr_tracking_bal.DeleteStateMsg DeleteEmpresa(string[] ID_EMPRESA)
        {
            elr_tracking_bal.DeleteStateMsg delete;
            string join;
            if (ID_EMPRESA == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_EMPRESA);
            }
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_DELETE_CAT_EMPRESA '" + join + "'");
                delete = new elr_tracking_bal.DeleteStateMsg("Registro eliminado" + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new elr_tracking_bal.DeleteStateMsg("Error al eliminar" + ex, false);
                return delete;
            }
        }
        /// <summary>
        /// Método para actualizar una Empresa
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg UpdateEmpresa(Empresa cat)
        {
            Empresa update;
            UpsertStateMsg updateCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_UPDATE_CAT_EMPRESA " + cat.ID_EMPRESA + ",'" + cat.EMPRESA_NAME + "','" +cat.RAZON_SOCIAL+"','"+ cat.RFC+"','"+cat.DIRECCION+"'");

                update = new  Empresa(cat.ID_EMPRESA, cat.EMPRESA_NAME,cat.DIRECCION,cat.RAZON_SOCIAL,cat.RFC);
                updateCat = new UpsertStateMsg("Registros actualizados " + dt.ToString(), true);
                return updateCat;
            }
            catch (Exception ex)
            {
                updateCat = new UpsertStateMsg("Error al actualizar " + ex, false);
                return updateCat;
            }
        }
        /// <summary>
        /// Método para obtener una empresa
        /// </summary>
        /// <param name="ID_EMPRESA"></param>
        /// <returns></returns>
        public static Empresa GetOneEmpresa(int ID_EMPRESA)
        {
            try
            {
                Empresa cat;
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETONE_CAT_EMPRESA " + ID_EMPRESA + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (ID_EMPRESA == 0)
                {
                    return null;
                }
                else
                {
                    cat = new Empresa(Convert.ToInt32(dt.Rows[0]["ID_EMPRESA"]), Convert.ToString(dt.Rows[0]["EMPRESA_NAME"]),Convert.ToString(dt.Rows[0]["RAZON_SOCIAL"]),Convert.ToString(dt.Rows[0]["RFC"]),Convert.ToString(dt.Rows[0]["DIRECCION"]));
                }
                return cat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Constructor sin parámetros
        /// </summary>
        public Empresa() { }
        /// <summary>
        /// Constructor con cuatro parámetros
        /// </summary>
        /// <param name="EMPRESA_NAME"></param>
        /// <param name="RAZON_SOCIAL"></param>
        /// <param name="RFC"></param>
        /// <param name="DIRECCION"></param>
        public Empresa(string EMPRESA_NAME, string RAZON_SOCIAL, string RFC, string DIRECCION)
        {
            this._EMPRESA_NAME = EMPRESA_NAME;
            this._RAZON_SOCIAL = RAZON_SOCIAL;
            this._RFC = RFC;
            this._DIRECCION = DIRECCION;
        }
        /// <summary>
        /// Constructor con cinco parámetros
        /// </summary>
        /// <param name="ID_EMPRESA"></param>
        /// <param name="EMPRESA_NAME"></param>
        /// <param name="RAZON_SOCIAL"></param>
        /// <param name="RFC"></param>
        /// <param name="DIRECCION"></param>
        public Empresa(int ID_EMPRESA, string EMPRESA_NAME, string RAZON_SOCIAL, string RFC, string DIRECCION)
        {
            this._ID_EMPRESA = ID_EMPRESA;
            this._EMPRESA_NAME = EMPRESA_NAME;
            this._RAZON_SOCIAL = RAZON_SOCIAL;
            this._RFC = RFC;
            this._DIRECCION = DIRECCION;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}